package com.cloudxingkong.dianyinke.wxapi;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.config.Constans;
import com.cloudxingkong.dianyinke.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信支付成功
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private IWXAPI wxapi;
    private TextView tvIsSuccess;
    private TextView tvOrderNo;
    private ImageView ivSuccesss;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        wxapi = WXAPIFactory.createWXAPI(this, Constans.APP_ID);
        wxapi.handleIntent(getIntent(),this);
        initView();
    }

    private void initView() {
        tvIsSuccess = (TextView) findViewById(R.id.we_issuccess);
        tvOrderNo = (TextView) findViewById(R.id.we_order_num);
        ivSuccesss = (ImageView) findViewById(R.id.we_ico_chenggong);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        wxapi.handleIntent(intent,this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e("test", "微信支付结果 " + baseResp.errCode + "\n信息 " + baseResp.errStr);
            Message message = handler.obtainMessage();
            message.what=0;
            message.arg1=baseResp.errCode;
            handler.sendMessage(message);
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                int errCode=msg.arg1;
                if (errCode == 0) {
                    Toast.makeText(WXPayEntryActivity.this, "恭喜您支付成功！", Toast.LENGTH_SHORT).show();
                    //TODO 支付成功后设置订单号
                    tvOrderNo.setVisibility(View.VISIBLE);
                }else if (errCode == -1) {
                    ivSuccesss.setVisibility(View.GONE);
                    tvIsSuccess.setText("支付失败");
                    tvOrderNo.setVisibility(View.GONE);
                    Toast.makeText(WXPayEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                }else if (errCode == -2) {
                    ivSuccesss.setVisibility(View.GONE);
                    tvIsSuccess.setText("您已取消支付");
                    tvOrderNo.setVisibility(View.GONE);
                    Toast.makeText(WXPayEntryActivity.this, "您已取消支付", Toast.LENGTH_SHORT).show();
                }
            }
        }
    };

    public void back(View view) {
        finish();
    }
}
